#!/bin/bash

path_root=$(pwd)
PATH=/usr/bin:/bin:$path_root/../../function:$path_root/function:$path_root
function echo_shrc(){
    if [ ! "$1" ];then
	    return 1
    else
        if [ ! "$(cat ~/.*shrc | grep "$1")" ];then
            echo $1 >> ~/.*shrc
        fi
    fi
    return 0
}
read -p "请输入本机cpu类型(x86,x64,arm)：" CpuType

ROOT=~/esp/environment
echo 创建目录
mkdir -p $ROOT/toolchain
mkdir -p $ROOT/bin
mkdir -p ~/esp/esp32/components
mkdir -p ~/esp/esp32/project
if [ $CpuType = "x86" -o $CpuType = "x64" ]; then
    mkdir -p ~/esp/esp8266/components
    mkdir -p ~/esp/esp8266/project
fi

echo 
echo 
echo 添加必要软件包 axel gcc git wget make libncurses-dev flex bison gperf python python-pip python-setuptools python-serial python-cryptography python-future python-pyparsing python-pyelftools
sudo apt-get install -y axel gcc git wget make libncurses-dev flex bison gperf python python-pip python-setuptools python-serial python-cryptography python-future python-pyparsing python-pyelftools
echo 
echo 
echo 添加 esp32[gcc 8.20] '&' esp8266[gcc 5.20] 编译的工具链 
cd $ROOT/toolchain
if [ $CpuType = "arm" ]; then
    axel -a -n 100 -o esp32.tar.xz https://dl.espressif.com/dl/xtensa-esp32-elf-gcc8_2_0-esp32-2019r1-rc2-linux-armel.tar.xz
elif [ $CpuType = "x64" ]; then
    axel -a -n 100 -o esp32.tar.xz https://dl.espressif.com/dl/xtensa-esp32-elf-gcc8_2_0-esp32-2019r1-rc2-linux-amd64.tar.xz
    axel -a -n 100 -o esp8266.tar.gz https://dl.espressif.com/dl/xtensa-lx106-elf-linux64-1.22.0-92-g8facf4c-5.2.0.tar.gz
elif [ $CpuType = "x86" ]; then
    axel -a -n 100 -o esp32.tar.xz https://dl.espressif.com/dl/xtensa-esp32-elf-gcc8_2_0-esp32-2019r1-rc2-linux-i686.tar.xz
    axel -a -n 100 https://dl.espressif.com/dl/xtensa-lx106-elf-linux32-1.22.0-92-g8facf4c-5.2.0.tar.gz
fi
tar -xvJf $ROOT/toolchain/esp32.tar.xz -C $ROOT/toolchain/
if [ $CpuType = "x86" -o $CpuType = "x64" ]; then
    tar -zvxf $ROOT/toolchain/esp8266.tar.gz -C $ROOT/toolchain/
fi
echo 
echo
echo 添加 esp-idf SDK "&" esp8266-idf SDK 到 $ROOT
echo 由于这是第一次部署环境脚本 ， 因此 SDK 需要下载的 ， 确保不会出现 SDK 文件缺失引起的问题.
cd ~/esp/esp32
git clone --recursive https://github.com/espressif/esp-idf.git esp-idf
if [ $CpuType = "x86" -o $CpuType = "x64" ]; then
    cd ~/esp/esp8266
    echo
    git clone --recursive https://github.com/espressif/ESP8266_RTOS_SDK.git esp-idf
    echo 
fi
echo 
echo 安装依赖的 Python 软件包
echo
python -m pip install --user -r ~/esp/esp32/esp-idf/requirements.txt
echo 
echo 

# creak esp-make script's file
cat>$ROOT/bin/esp-make<<EOF
#!/bin/bash
make -j$(( `cat /proc/cpuinfo| grep "processor"| wc -l` * 2 )) flash monitor
exit 0
EOF
# creak esp-make-app script's file
cat>$ROOT/bin/esp-make-app<<EOF
#!/bin/bash
make -j$(( `cat /proc/cpuinfo| grep "processor"| wc -l` * 2 )) app-flash monitor
exit 0
EOF



#   环境变量添加：
if [ $CpuType = "x86" -o $CpuType = "x64" ]; then
    echo_shrc "ESP8266ROOT=~/esp/esp8266"
    echo_shrc "ESPROOT=\$ESP8266ROOT"
    echo_shrc "PATH=\$PATH:~/esp/environment/toolchain/xtensa-lx106-elf/bin"
    # creak esp-change script's file
    cat>$ROOT/bin/esp-change<<EOF
#!/bin/bash
if [ "\$1" = "32" ];then
    sed -i "s/ESPROOT=\$ESP8266ROOT/ESPROOT=\$ESP32ROOT/g"  \`grep ESPROOT=\$ESP8266ROOT -rl ~/.*shrc\`
else
    sed -i "s/ESPROOT=\$ESP32ROOT/ESPROOT=\$ESP8266ROOT/g"  \`grep ESPROOT=\$ESP32ROOT -rl ~/.*shrc\`
fi
exit 0
EOF
else
    echo_shrc "ESP32ROOT=~/esp/esp32"
    echo_shrc "ESPROOT=\$ESP32ROOT"
fi
echo_shrc "PATH=\$PATH:~/esp/environment/toolchain/xtensa-esp32-elf/bin"
echo_shrc "PATH=\$PATH:~/esp/environment/bin"
echo_shrc "IDF_PATH=\$ESPROOT/esp-idf"
#   收尾
chmod +x $ROOT/bin/*
# rm $ROOT/toolchain/*.tar.gz
exit 0